const resolve = require("path").resolve;
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const url = require("url");
const publicPath = "/group";
const CopyPlugin = require("copy-webpack-plugin");

module.exports = (options = {}) => {
    const BASE_URL = options.dev ? "/assets/" : publicPath;
    return {
        entry: {
            vendor: "./src/vendor",
            index: "./src/main.js",
            // component: './src/Component.js'
        },
        output: {
            path: resolve(__dirname, "dist"),
            filename: options.dev ? "[name].js" : "[name].js?[chunkhash]",
            chunkFilename: "[id].js?[chunkhash]",
            publicPath: BASE_URL,
        },
        module: {
            rules: [{
                test: /\.vue$/,
                loader: "vue-loader",
                options: {
                    loaders: {
                        scss: "vue-style-loader!css-loader!px2rem-loader?remUnit=192!sass-loader",
                    },
                },
            },
                // {
                //   test: /\.js$/,
                //   use: [{
                //     loader: 'babel-loader',
                //     options: {
                //       presets: ['es2015'],
                //       plugins: ['syntax-dynamic-import']
                //     },
                //   }],
                // },
                {
                    test: /\.js$/,
                    use: ["babel-loader"],
                    exclude: /node_modules/,
                },
                {
                    test: /\.scss$/,
                    use: ["style-loader", "css-loader", "postcss-loader", {
                        loader: "px2rem-loader",
                        options: {
                            remUnit: 192,
                        },
                    }, "sass-loader"],
                },
                {
                    test: /\.css$/,
                    use: ["style-loader", "css-loader", "postcss-loader", {
                        loader: "px2rem-loader",
                        options: {
                            remUnit: 192,
                        },
                    }],
                },
                {
                    test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
                    use: [{
                        loader: "url-loader",
                        options: {
                            limit: 10000,
                        },
                    }],
                },
                {
                    test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
                    use: [{
                        loader: "url-loader",
                        options: {
                            limit: 10000,
                        },
                    }],
                },
            ],
        },
        plugins: [
            new webpack.optimize.CommonsChunkPlugin({
                names: ["vendor", "manifest"],
            }),
            new HtmlWebpackPlugin({
                template: "src/index.html",
                templateParameters: (compilation, assets, pluginOptions) => {
                    // enhance html-webpack-plugin's built in template params
                    let stats;
                    return Object.assign({
                        // make stats lazy as it is expensive
                        get webpack() {
                            return stats || (stats = compilation.getStats().toJson());
                        },
                        compilation: compilation,
                        webpackConfig: compilation.options,
                        htmlWebpackPlugin: {
                            files: assets,
                            options: pluginOptions,
                        },
                    }, {
                        BASE_URL: BASE_URL,
                    });
                },
            }),
            new CopyPlugin([
                {from: "static", to: "./", toType: "dir"},
            ]),
            new webpack.DefinePlugin({
                "process.env.BASE_URL": JSON.stringify(BASE_URL),
            }),
        ],
        resolve: {
            alias: {
                "~": resolve(__dirname, "src"),
                "@": resolve(__dirname, "src/views/wutu"),
            },
            extensions: [".js", ".vue", ".json", ".css", ".scss"],
        },
        devServer: {
            host: "127.0.0.1",
            port: 8089,
            proxy: {
                "/api/": {
                    target: "http://127.0.0.1:8080",
                    changeOrigin: true,
                    pathRewrite: {
                        "^/api": "",
                    },
                },
            },
            historyApiFallback: {
                index: url.parse(options.dev ? "/assets/" : publicPath).pathname,
            },
        },
        devtool: options.dev ? "#eval-source-map" : "#source-map",
    };
};
